/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.engine.filter.ZYFilter;
import org.apache.dubbo.engine.loadbalance.FirstLoadBalance;
import org.apache.dubbo.engine.protocol.ZYProtocol;

/**
 * @author zhuge
 */
public class ApplicationConsumer {
    /**
     * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
     * launch the application
     */
    public static void main(String[] args) throws InterruptedException {
//        log4j.rootLogger=error, stdout  将error改为info可以看到更多调试信息
        ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-api-consumer");
        applicationConfig.setQosEnable(false);
        reference.setApplication(applicationConfig);
        reference.setRegistry(new RegistryConfig("zookeeper://172.20.59.116:2181"));
        reference.setInterface(DemoService.class);


        //依赖使用dubbo引擎
        reference.setLoadbalance(FirstLoadBalance.NAME);

        //消费者和提供要使用相同的协议,否则不能正常通信
        reference.setProtocol(ZYProtocol.NAME);
        reference.setFilter(ZYFilter.NAME);

        DemoService service = reference.get();
        while (true) {
            String message = service.sayHello("dubbo");
            System.out.println(message);
            Thread.sleep(1000);
        }
    }
}
